/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "DXMaterial.h"

namespace directx {

DXMaterial::DXMaterial(void)
{
	release();
}

DXMaterial::~DXMaterial(void)
{
	release();
}

void DXMaterial::release()
{
	ZeroMemory( &m_dxMaterial, sizeof(D3DMATERIAL9) );
}

void DXMaterial::reset()
{
	// Get the d3dDevice -> see DXResource
	DXResource::reset();
}

bool DXMaterial::bind()
{
	if (FAILED( pd3dDevice->SetMaterial( &m_dxMaterial ) ))
	{
		coreGetLog()->WriteError("Failed to bind DXMaterial", __FILE__, __LINE__);
		return false;
	}
	if (FAILED( pd3dDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL) ))
	{
		coreGetLog()->WriteError("Failed to set render state", __FILE__, __LINE__);
		return false;
	}

	return true;
}

bool DXMaterial::unbind()
{
	return false;
}

void DXMaterial::setDiffuse(const math::Vector4 &color)
{
	m_dxMaterial.Diffuse.r = color.x;
	m_dxMaterial.Diffuse.g = color.y;
	m_dxMaterial.Diffuse.b = color.z;
	m_dxMaterial.Diffuse.a = color.w;
}

void DXMaterial::setAmbient(const math::Vector4 &color)
{
	m_dxMaterial.Ambient.r = color.x;
	m_dxMaterial.Ambient.g = color.y;
	m_dxMaterial.Ambient.b = color.z;
	m_dxMaterial.Ambient.a = color.w;
}

void DXMaterial::setSpecular(const math::Vector4 &color)
{
	m_dxMaterial.Specular.r = color.x;
	m_dxMaterial.Specular.g = color.y;
	m_dxMaterial.Specular.b = color.z;
	m_dxMaterial.Specular.a = color.w;
}

void DXMaterial::setEmissive(const math::Vector4 &color)
{
	m_dxMaterial.Emissive.r = color.x;
	m_dxMaterial.Emissive.g = color.y;
	m_dxMaterial.Emissive.b = color.z;
	m_dxMaterial.Emissive.a = color.w;
}

void DXMaterial::setPower(float power)
{
	m_dxMaterial.Power = power;
}

D3DMATERIAL9* DXMaterial::getDXMaterial()
{
	return &m_dxMaterial;
}

} // namespace
